'use strict';

import {
  SmzdmRss
} from './SmzdmRss';

const BaseBot = require('bot-sdk');

class Bot extends BaseBot {
  constructor(postData) {
    super(postData);

    this.addLaunchHandler(() => {
      this.waitAnswer();
      return {
        outputSpeech: '欢迎使用张大妈!'
      };
    });

    this.addSessionEndedHandler(() => {
      this.endSession();
      return {
        outputSpeech: '谢谢使用张大妈',
      };
    });

    this.addIntentHandler('smzdm.help', () => {
      return {
        outputSpeech: '你可以使用的指令为：优惠精选、海淘专区、原创频道、发现频道、资讯专区。并可以通过上一页、下一页进行翻页浏览。'
      };
    });

    this.addIntentHandler('smzdm.channel', () => {
      let channel = this.getSlot('rss_channel');
      let rss = new SmzdmRss(channel);
      let tts = '';
      let pageInfo = {};

      rss.load();
      pageInfo = rss.pager();
      if (rss._error) {
        tts = rss._error;
        return {
          outputSpeech: tts
        };
      }

      this.waitAnswer();

      this.setSessionAttribute('channel', channel);
      this.setSessionAttribute('page', 1);

      tts += `当前查询到${pageInfo.pages}页记录。`;

      if (pageInfo.items.length > 0) {
        tts += pageInfo.items.join('、');
      } else {
        tts = '数据暂未查到!';
      }

      return {
        outputSpeech: tts
      };
    });

    // tts = JSON.stringify(this.nlu);
    // return {
    //   outputSpeech: tts
    // };

    this.addIntentHandler('smzdm.channel.pager', () => {
      let channel = this.getSessionAttribute('channel', 'feed');
      let page = this.getSessionAttribute('page', 1);
      if (!channel) channel = 'feed';

      let direction = this.getSlot('page_direction');
      let page_offset = parseInt(this.getSlot('page_offset'));
      let tts = '';

      let rss = new SmzdmRss(channel);
      page = rss.getPageNo(page, page_offset, direction);

      if (page <= 0) {
        tts = '已经到最开始位置了，请浏览下一页！';
        return {
          outputSpeech: tts
        };
      }

      this.waitAnswer();
      this.setSessionAttribute('channel', channel);
      this.setSessionAttribute('page', page);

      let pageInfo = {};
      rss.load();
      pageInfo = rss.pager(page);
      if (rss._error) {
        tts = rss._error;
        return {
          outputSpeech: tts
        };
      }

      tts += `第${pageInfo.page}页。`;
      if (pageInfo.items.length > 0) {
        tts += pageInfo.items.join('、');
      } else {
        tts = '数据暂未查到!';
      }

      return {
        outputSpeech: tts
      };
    });

    this.addIntentHandler('ai.dueros.common.next_intent', () => {
      let channel = this.getSessionAttribute('channel', 'feed');
      let page = this.getSessionAttribute('page', 1);
      if (!channel) channel = 'feed';

      let direction = 'next';
      let page_offset = 1;
      let tts = '';

      let rss = new SmzdmRss(channel);
      page = rss.getPageNo(page, page_offset, direction);

      if (page <= 0) {
        tts = '已经到最开始位置了，请浏览下一页！';
        return {
          outputSpeech: tts
        };
      }

      this.waitAnswer();
      this.setSessionAttribute('channel', channel);
      this.setSessionAttribute('page', page);

      let pageInfo = {};
      rss.load();
      pageInfo = rss.pager(page);
      if (rss._error) {
        tts = rss._error;
        return {
          outputSpeech: tts
        };
      }

      tts += `第${pageInfo.page}页。`;
      if (pageInfo.items.length > 0) {
        tts += pageInfo.items.join('、');
      } else {
        tts = '数据暂未查到!';
      }

      return {
        outputSpeech: tts
      };
    });

  }
}

export default Bot;
export {
  Bot
};
